#include "httplib.h"
#include <iostream>
using std::cout;
using std::endl;
using std::cerr;
std::string GetFileContent(const std::string& path)
{
    std::fstream fs(path,std::ios::binary | std::ios::in);
    if(!fs.is_open())
    {
        return "";
    }

    fs.seekg(0,fs.end);
    int filesize = fs.tellg();
    fs.seekg(0,fs.beg);
    
    std::string content;
    content.resize(filesize);
    fs.read((char*)content.c_str(),content.size()); 
    fs.close();
    return content;
}
int main()
{
    // HTTP
    //对于没有设定的请求，httplib是接收到error_handdle里面处理了
    httplib::Server svr;
    

    svr.Get("/", [](const httplib::Request &req , httplib::Response &res) {
        std::cout<<"这是执行体\n";
        res.status = 200;
        res.set_content(GetFileContent("./wwwroot/Home.html"),"text/html");

    });
    svr.set_pre_routing_handler([](const httplib::Request &req, httplib::Response &res) ->httplib::Server::HandlerResponse{
        std::cout<<"这是预设\n";
        return httplib::Server::HandlerResponse::Handled;
    });
    svr.set_post_routing_handler([](const httplib::Request &req, httplib::Response &res) ->httplib::Server::HandlerResponse{
        std::cout<<"这是后置\n";
        return httplib::Server::HandlerResponse::Handled;
    });
    svr.set_error_handler([](const httplib::Request &req, httplib::Response &res) {
        std::cout<<"这是错误处理\n";
    });
    svr.listen("0.0.0.0",8888);

    return 0;
}